#include <iostream>
#include <stdio.h>

/*
 * 多重继承无函数覆盖
 */

class Base1
{
	public:
		virtual void function_1()
		{
			printf("Base1 function_1...\n");
		}

		virtual void function_2()
		{
			printf("Base1 function_2...\n");
		}
	
};

class Base2 : public Base1
{
	public:
		virtual void function_3()
		{
			printf("Base2 function_3...\n");
		}

		virtual void function_4()
		{
			printf("Base2 function_4...\n");
		}
	
};


class Sub :  Base2
{
	public:
		virtual void function_5()
		{
			printf("Sub function_5...\n");
		}

		virtual void function_6()
		{
			printf("Sub function_6...\n");
		}
};

int main(int argc, char ** argv)
{
	Sub sub;

	printf("size = %ld\n", sizeof(class Sub));	// 4。 直接继承一个父类，所以只有一个虚函数表

	printf("sub address is 0x%lx.\n", (long)&sub);		// this 指针，是堆栈里面的地址。虚表的地址不是this指针，是this指针的前四个字节。

	/* 对象的前4字节是第一个Base1的虚表 */
	printf("sub virtual table address is:0x%lx\n", *(long*)&sub); 
	printf("sub virtual table address is:%p\n", (void*)*(long*)&sub); 

	typedef void(*pfunction)(void);
	
	pfunction pf;

	for (int i = 0; i < 6; i++) {
		long temp = *((long*)(*(long*)&sub) + i);	
		if (temp == 0) {
			break;
		}

		pf = (pfunction)temp;
		pf();
	}

	return 0;
}

/*
	sub address is bfa78630.
	sub virtual table address is:8048a48
	Base1 function_1...
	Base1 function_2...
	Base2 function_3...
	Base2 function_4...
	Sub function_5...
	Sub function_6...
 */

#if 0

1.覆盖的是哪个在哪个虚表里；
2.。
3.。

sub对象

	---------------			-----------------
	| 0x123245    |	--------->    	| 0x123456	|	Base1:function_1
	---------------			-----------------
	| ....        |			| 0x123457	|	Base1:function_2
	---------------			-----------------
	| ....	      |			| 0x123458	|	Base2:function_3
	---------------			-----------------
	| ....        |			| 0x123457	|	Base2:function_4
	---------------			-----------------
	| ....	      |			| 0x123457	|	Sub:function_5
	---------------			-----------------
	| 其它成员     |			| 0x123457	|	Sub:function_6
	---------------			-----------------
	| ....	      |			| 0x000000	|
	---------------			-----------------

#endif
